{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1, -1,  0, -1,  0,  0, -1,  0, -1,  0])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#生成四个数列\n",
    "nums1 = np.random.randint(-2,2,(10,))\n",
    "nums2 = np.random.randint(-2,2,(10,))\n",
    "nums3 = np.random.randint(-2,2,(10,))\n",
    "nums4 = np.random.randint(-2,2,(10,))\n",
    "nums4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def algorithm(nums1, nums2, nums3, nums4):\n",
    "    record = dict()\n",
    "    for n1 in nums1:\n",
    "        for n2 in nums2:\n",
    "            if n1 + n2 in record:\n",
    "                record[n1+n2] += 1\n",
    "            else:\n",
    "                record[n1+n2] = 1       # 遍历前两个数组，将其和存入字典。字典值记录相同和的次数：对应每个位置的和\n",
    "    count = 0\n",
    "    for n3 in nums3:\n",
    "        for n4 in nums4:\n",
    "            if -n3-n4 in record:        # 判断后两个数组和的相反数在字典里吗，在的话说明满足和为0的题目要求\n",
    "                count +=1\n",
    "    return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "algorithm(nums1, nums2, nums3, nums4)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
